package net.huashitong.supervision.gongjian.service.project;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.gongjian.dao.ZaijianTisuDao;
import net.huashitong.supervision.gongjian.entity.project.ZaijianTisuEntity;
import net.huashitong.supervision.gongjian.entity.project.ZhengQianEntity;
import net.huashitong.supervision.gongjian.filter.ZaijianTisuFilter;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectFeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ZaijianTisuService extends StringPKService<ZaijianTisuEntity> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private ZaijianTisuDao dao;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private ProjectFeedBackService feedBackService;

    @Override
    protected BaseDao<ZaijianTisuEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(ZaijianTisuEntity obj) {
        String sysId = tableService.updateMaxSysId("t_gj_zaijiantisu", null);
        obj.setSysId(sysId);
    }
    /**
     设置当期填报状态
     */
    private ZaijianTisuEntity setState(ZaijianTisuEntity entity, int vaild , int pType){
        int feedbackCount = feedBackService.getFeedbackCount(entity.getSysId(), vaild, pType);
        if(1<=feedbackCount){
            entity.setCurrentBack(1);
        }else {
            entity.setCurrentBack(0);
        }
        return entity;
    }

    public List<ZaijianTisuEntity> getList(ZaijianTisuFilter filter)
    {
        List<ZaijianTisuEntity> list = dao.getList(filter);
        for (ZaijianTisuEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 3);
            setState(entity,1,3);
            entity.setZerenDept(projectDeptName);
        }
        return list;
    }

    public List<ZaijianTisuEntity> getListForOwn(ZaijianTisuFilter filter)
    {
        filter.setProjectType(3);
        List<ZaijianTisuEntity> list = dao.getListForOwn(filter);
        for (ZaijianTisuEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 3);
            setState(entity,1,3);
            entity.setZerenDept(projectDeptName);
        }
        return list;
    }

    public List<ZaijianTisuEntity> getListNoPage(ZaijianTisuFilter filter)
    {
        List<ZaijianTisuEntity> list = dao.getListNoPage(filter);

        return list;
    }
}
